package com.system.service;

import com.system.domain.APPIDConfig;
import com.system.domain.SysPrinterServerBo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.*;

import javax.annotation.Resource;


@Component
public class StartupRequestRunner implements CommandLineRunner {

    private final RestTemplate restTemplate;
    private final SysprintergentSN sysprintergentSN;

    @Resource
    private APPIDConfig appIdConfig;

    public StartupRequestRunner(RestTemplate restTemplate, PrinterService printerService, SysprintergentSN sysprintergentSN) {
        this.restTemplate = restTemplate;
        this.sysprintergentSN = sysprintergentSN;
    }

    @Override
    public void run(String... args) throws Exception {
        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求体
        SysPrinterServerBo bo = sysprintergentSN.getWindowsSN();
        // 初始化 bo 对象的属性
        // bo.setXXX(xxx);

        // 创建 HttpEntity 对象，包含请求头和请求体
        HttpEntity<SysPrinterServerBo> requestEntity = new HttpEntity<>(bo, headers);

        // 定义请求的 URL
//        String url = "http://ticket.cinorecycle:8888/system/printerServer";
//        String url = "http://localhost:8888/system/printerServer";
//        String url = "http://139.196.241.152:8888/system/printerServer";
//        String url = "http://121.5.163.108:8888/system/printerServer";//测试服，自动注册
        String url = appIdConfig.getUrl()+"/system/printerServer";

        // 发送 POST 请求
        ResponseEntity<Void> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                Void.class
        );


        // 处理响应
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println("连接成功");
        } else {
            System.out.println("注册失败，状态码: " + response.getStatusCode());
        }
    };




}